﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL_MechSysWeb;
using DTO_MechSysWeb;
using System.Web.Security;
using System.Data;

namespace WEB_MechSysWeb.Paginas_Administrador
{
    public partial class CurriculosPendentes : System.Web.UI.Page
    {   
        BLLCurriculo bllCurriculo = new BLLCurriculo();
        BLLEndereco bllEndereco = new BLLEndereco();
        MembershipUser memberShipUser;
        static int curriculoID;
        List<DTOCurriculo> lstCurriculosRecebidos = new List<DTOCurriculo>();

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                memberShipUser = Membership.GetUser(User.Identity.Name);
                if (memberShipUser != null)
                {
                    if (Roles.IsUserInRole("Cliente"))
                    {
                        Response.Redirect("~/Paginas_Logado/ClientePerfil.aspx");

                    }
                    if (Roles.IsUserInRole("Administrador"))
                    {


                    }
                }
                else
                {
                    Response.Redirect("~/Paginas_Publico/PagLogin.aspx");
                }
            }
            catch (Exception)
            {
                Response.Redirect("~/Paginas_Publico/PagLogin.aspx");
            }

            PreecherDados();
        }

        public void LimparCampos()
        {
            txtNome.Text = string.Empty;
            txtSobrenome.Text = string.Empty;
            txtCPF.Text = string.Empty;
            txtDataNascimento.Text = string.Empty;
            txtTelefone.Text = string.Empty;
            txtCelular.Text = string.Empty;
            txtEmail.Text = string.Empty;

            txtCEP.Text = string.Empty;
            txtRua.Text = string.Empty;
            txtNumero.Text = string.Empty;
            txtComplemento.Text = string.Empty;
            txtBairro.Text = string.Empty;
            txtCidade.Text = string.Empty;      

            txtFacebook.Text = string.Empty;
            txtLinkedin.Text = string.Empty;
            txtAreas.Text = string.Empty;
            txtQualificacoes.Text = string.Empty;
            txtObjetivos.Text = string.Empty;         

        }
          
        public void PreecherDados()
        {
            try
            {
                lstCurriculosRecebidos = bllCurriculo.BuscarCurriculosRecebidos();
                gridCurriculos.DataSource = PopulaGridCurriculos(lstCurriculosRecebidos);
                gridCurriculos.DataBind();
            }
            catch (Exception)
            {
                Mensagem.ExibirEmUP("Não foi possível carregar dados!");
            }
        }

        public DataTable PopulaGridCurriculos(List<DTOCurriculo> lstCurriculos)
        {
            DataTable dataTableGridView = new DataTable();
            DataRow dataRow;

            try
            {
                if (lstCurriculos.Count > 0)
                {

                    dataTableGridView.Columns.Add("CurriculoID", typeof(string));
                    dataTableGridView.Columns.Add("Nome", typeof(string));
                    dataTableGridView.Columns.Add("Sexo", typeof(string));
                    dataTableGridView.Columns.Add("DataNascimento", typeof(string));
                    dataTableGridView.Columns.Add("Facebook", typeof(string));
                    dataTableGridView.Columns.Add("Telefone", typeof(string));

                    foreach (DTOCurriculo dtoCurriculo in lstCurriculos)
                    {
                        dataRow = dataTableGridView.NewRow();

                        dataRow["CurriculoID"] = dtoCurriculo.CurriculoID.ToString();
                        dataRow["Nome"] = string.Format("{0} {1}", dtoCurriculo.Nome, dtoCurriculo.Sobrenome);                      
                        dataRow["Sexo"] = dtoCurriculo.Sexo.ToString();
                        dataRow["DataNascimento"] = string.Format("{0:d}", dtoCurriculo.DataNascimento);
                        dataRow["Facebook"] = dtoCurriculo.Facebook;                      
                        dataRow["Telefone"] = dtoCurriculo.Telefone;

                        dataTableGridView.Rows.Add(dataRow);

                    }
                }

            }
            catch (Exception)
            {
                Mensagem.ExibirEmUP("Não foi possível carregar os dados para o grid!");
            }

            return dataTableGridView;
        }      
    
        protected void gridCurriculos_SelectedIndexChanged(object sender, EventArgs e)
        {
            int enderecoID;

            curriculoID = Convert.ToInt32(gridCurriculos.SelectedValue.ToString());

            DTOCurriculo dtoCurriculo = bllCurriculo.BuscarCurriculoPorCurriculoID(curriculoID);

            ImagemSelecionado.DataSource = bllCurriculo.BuscarCurriculosRecebidos();
            ImagemSelecionado.DataBind();

            txtNome.Text = dtoCurriculo.Nome;
            txtSobrenome.Text = dtoCurriculo.Sobrenome;
            rblSexo.SelectedValue = dtoCurriculo.Sexo.ToString();
            txtCPF.Text = dtoCurriculo.CPF;
            ddlEstadoCivil.SelectedValue = dtoCurriculo.EstadoCivil.ToString();
            txtDataNascimento.Text = dtoCurriculo.DataNascimento.ToShortDateString();
            txtTelefone.Text = dtoCurriculo.Telefone;
            txtCelular.Text = dtoCurriculo.Celular;
            txtEmail.Text = dtoCurriculo.Email;
            txtFacebook.Text = dtoCurriculo.Facebook;
            txtLinkedin.Text = dtoCurriculo.Linkedin;
            ddlEscolaridade.SelectedValue = dtoCurriculo.Escolaridade.ToString();
            txtAreas.Text = dtoCurriculo.AreaInteresse;
            txtQualificacoes.Text = dtoCurriculo.Qualificacao;
            txtObjetivos.Text = dtoCurriculo.Objetivo;

            enderecoID = dtoCurriculo.EnderecoID;

            DTOEndereco dtoEndereco = bllEndereco.BuscarEnderecoPorEnderecoID(enderecoID);

            txtRua.Text = dtoEndereco.Rua;
            txtNumero.Text = dtoEndereco.Numero;
            txtBairro.Text = dtoEndereco.Bairro;
            txtComplemento.Text = dtoEndereco.Complemento;
            txtCidade.Text = dtoEndereco.Cidade;
            ddlEstado.SelectedValue = dtoEndereco.Estado.ToString();
            txtCEP.Text = dtoEndereco.CEP;
         

        }
    }

}